package com.xxjqr.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxjqr.rbac.entity.pojo.TbDict;
import com.xxjqr.rbac.entity.vo.DictMapVo;
import com.xxjqr.rbac.entity.vo.DictVo;
import com.xxjqr.rbac.enums.StatusEnum;
import com.xxjqr.rbac.mapper.TbDictMapper;
import com.xxjqr.rbac.service.TbDictService;
import com.xxjqr.rbac.util.RedisUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xxjqr.rbac.constant.Const.DICT_KEY;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 丁昌江
 * @since 2021-02-02
 */
@Service
@AllArgsConstructor
public class TbDictServiceImpl extends ServiceImpl<TbDictMapper, TbDict> implements TbDictService {

    private final TbDictMapper dictMapper;
    private final RedisUtil redisUtil;

    @Override
    @SuppressWarnings("unchecked")
    public HashMap<String, List<DictVo>> getAllDictMap() {
        List<DictMapVo> allDictMap = null;
        Object object = redisUtil.get(DICT_KEY);
        if (object != null) {
            allDictMap = (List<DictMapVo>) object;
        }
        if (allDictMap == null) {
            allDictMap = dictMapper.getAllDictMap();
            redisUtil.set(DICT_KEY, allDictMap);
        }
        HashMap<String, List<DictVo>> dictMap = new HashMap<>();
        for (DictMapVo dictMapVo : allDictMap) {
            dictMap.put(dictMapVo.getType(),dictMapVo.getDictMaps());
        }
        return dictMap;
    }

}
